﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Diagnostics;


public partial class OutSourcingContratos : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["iduser"] == null || Session["iduser"].ToString() == "0")
            {
                Response.Redirect("index.aspx");
            }
            else
            {
                lblses.Text = "Bienvenido " + (string)(Session["Ncom"]);
                //llenamos el dataset con los datos de la tablas de empresas                                    
                //llenarficha();
            }
        }

    }
   
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            DataSet ds = new DataSet();
            Outs.Outsourcing em = new Outs.Outsourcing();
            ds = em.ConsultaOutSourcing();
            //creamos el objeto tabla para llenar una nueva fila
            DataTable TOut = ds.Tables["Contratos"];
            DataRow row = TOut.NewRow();
            //ingrasamos los datos de la fila
            row["IdContratos"] = ds.Tables["Contratos"].Rows.Count + 1;
            row["Cargo"] = TxtOutCargo.Text;
            row["TipoTrabajo"] = TxtOutipoTrabajo.Text;
            row["CodigoContrato"] = int.Parse(TxtCodCotrato.Text);
            row["MontoContrato"] = float.Parse(txtmonto.Text);
            row["FechaInicio"] = DateTime.Parse(TxtOutFechaInContra.Text);
            row["FechaFinal"] = DateTime.Parse(txtffinal.Text);
            row["IdOutsourcing"] = 1;
            row["IdTiposContrato"] = cmbTipoContrato.SelectedValue;


            ds.Tables["Contratos"].Rows.Add(row);

            Outs.Outsourcing cl = new Outs.Outsourcing();
            cl.DatoContratos(ds);
            ds.AcceptChanges();
            llenarficha();
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    }

 //--------------------------------------------------------------------------------------------------------------------------
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        if (LinkButton1.Text == "Actualizar")
        {
            lblG.Visible = false;
            btnG.Visible = false;
            LinkButton1.Text = "Guardar";
            lblA.Visible = true;
            btnA.Visible = true;
            cmbcont.Visible = true;
            TxtCodCotrato.Visible = false;
        }
        else
        {
            lblA.Visible = false;
            btnA.Visible = false;
            LinkButton1.Text = "Actualizar";
            lblG.Visible = true;
            btnG.Visible = true;
            cmbcont.Visible = false;
            TxtCodCotrato.Visible = true;

        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        try
        {
            String User = cmbcont.SelectedValue;
            DataSet dset = new DataSet();
            Usuarios.Usuario usu = new Usuarios.Usuario();
            dset = usu.estandarweb("Select * from Contratos where IdContratos = " + User, "Contratos");

            if (dset.Tables["Contratos"].Rows.Count > 0)
            {
                dset.Tables["Contratos"].Rows[0].Delete();

                Outs.Outsourcing cl = new Outs.Outsourcing();
                cl.DatoContratos(dset);
                dset.AcceptChanges();
                Response.Redirect("OutsourcingContratos.aspx");
            }
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    }
    //--------------------------------------------------------------------------------------------
    void llenarficha()
    {
        try
        {
            String User = cmbcont.SelectedValue;
            DataSet dset = new DataSet();
            Usuarios.Usuario usu = new Usuarios.Usuario();
            dset = usu.estandarweb("Select * from Contratos where IdContratos = " + User, "Contratos");

            if (dset.Tables["Contratos"].Rows.Count > 0)
            {
                LinkButton2.Visible = true;
                lblG.Visible = false;
                btnG.Visible = false;
                LinkButton1.Text = "Guardar";
                lblA.Visible = true;
                btnA.Visible = true;
                cmbcont.Visible = true;
                TxtCodCotrato.Visible = false;
                cmbcont.SelectedValue = dset.Tables["Contratos"].Rows[0]["IdContratos"].ToString();
                TxtOutCargo.Text = dset.Tables["Contratos"].Rows[0]["Cargo"].ToString();
                TxtOutipoTrabajo.Text = dset.Tables["Contratos"].Rows[0]["TipoTrabajo"].ToString();
                TxtCodCotrato.Text = dset.Tables["Contratos"].Rows[0]["CodigoContrato"].ToString();
                txtmonto.Text = dset.Tables["Contratos"].Rows[0]["MontoContrato"].ToString();
                TxtOutFechaInContra.Text = dset.Tables["Contratos"].Rows[0]["FechaInicio"].ToString();
                txtffinal.Text = dset.Tables["Contratos"].Rows[0]["FechaFinal"].ToString();
                cmbTipoContrato.SelectedValue = dset.Tables["Contratos"].Rows[0]["IdTiposContrato"].ToString();


            }
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    } 
  
    protected void cmbcont_SelectedIndexChanged(object sender, EventArgs e)
    {
        llenarficha();
    }
    protected void btnA_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            String User = cmbcont.SelectedValue;
            DataSet dset = new DataSet();
            Usuarios.Usuario usu = new Usuarios.Usuario();
            dset = usu.estandarweb("Select * from Contratos where IdContratos = " + User, "Contratos");

            if (dset.Tables["Contratos"].Rows.Count > 0)
            {
                dset.Tables["Contratos"].Rows[0].BeginEdit();
                dset.Tables["Contratos"].Rows[0]["IdContratos"] = User;
                dset.Tables["Contratos"].Rows[0]["Cargo"] = TxtOutCargo.Text;
                dset.Tables["Contratos"].Rows[0]["TipoTrabajo"] = TxtOutipoTrabajo.Text;
                dset.Tables["Contratos"].Rows[0]["CodigoContrato"] = int.Parse(TxtCodCotrato.Text);
                dset.Tables["Contratos"].Rows[0]["MontoContrato"] = float.Parse(txtmonto.Text);
                dset.Tables["Contratos"].Rows[0]["FechaInicio"] = DateTime.Parse(TxtOutFechaInContra.Text);
                dset.Tables["Contratos"].Rows[0]["FechaFinal"] = DateTime.Parse(txtffinal.Text);
                dset.Tables["Contratos"].Rows[0]["IdOutsourcing"] = 1;
                dset.Tables["Contratos"].Rows[0]["IdTiposContrato"] = cmbTipoContrato.SelectedValue;
                dset.Tables["Contratos"].Rows[0].EndEdit();
                //ingrasamos los datos de la fila

                Outs.Outsourcing cl = new Outs.Outsourcing();
                cl.DatoContratos(dset);
                dset.AcceptChanges();
                Response.Redirect("OutsourcingContratos.aspx");


            }
        }
        catch (Exception ex)
        {


            EventLog LogErr = new EventLog();
            if (!EventLog.SourceExists("WebContrataci"))
            {
                EventLog.CreateEventSource("WebContrataci", "Application");
                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString(), EventLogEntryType.Information, 1);
            }
            else
            {

                LogErr.Source = "WebContrataci";
                LogErr.WriteEntry(ex.ToString());
            }
        }
    }
}